package com.fzrskj.cloud.free.core.tool;

import ms.core.tool.RegularTool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * CRegularTool.
 *
 * @author zj.
 *         Created on 2018/9/3 0003.
 */
public class CRegularTool extends RegularTool {
    //检测是否包含中文
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * to verify ip address
     * @param ip ip address to be verified
     * @return true or false
     */
    public static boolean checkIp(String ip) {
        String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
        String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
        return match(regex, ip);
    }

    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }
}
